<html>
<head>
<script src="../../resources/js-test.js"></script>
<script src="resources/webgl-test.js"></script>
<script src="resources/webgl-test-utils.js"> </script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<script>
description("Checks that painting WebGL contents doesn't pollute the context state.");

var wtu = WebGLTestUtils;

if (window.testRunner) {
    testRunner.dumpAsText();
    testRunner.waitUntilDone();
}

if (window.internals)
    internals.settings.setWebGLErrorsToConsoleEnabled(false);

var canvas;
var context;

function draw() {
  // Draw a blue quad to the screen.  The compositor should have executed
  // before reaching this point.  If the texture state was corrupted, then
  // a red quad will be drawn.  See crbug.com/105045.
  wtu.drawQuad(context);

  wtu.checkCanvasRect(context, 0, 0, 1, 1, [0, 0, 255, 255], "Should have rendered blue.", 1);
  if (window.testRunner)
    testRunner.notifyDone();
}

window.onload = function()
{
  if (window.initNonKhronosFramework) {
    window.initNonKhronosFramework(false);
  }

  canvas = document.getElementById("webgl-canvas");
  context = create3DContext(canvas);

  var program = wtu.setupTexturedQuad(context);
  var bufferObjects = wtu.setupUnitQuad(context);
  var texture = wtu.createColoredTexture(context, 1, 1, [0, 0, 255, 255]);

  context.uniform1i(context.getUniformLocation(program, "tex"), 0);

  context.activeTexture(context.TEXTURE_0 + 5);
  context.bindTexture(context.TEXTURE_2D, texture);

  context.viewport(0, 0, canvas.width, canvas.height);

  context.clearColor(255, 0, 0, 255);
  context.clear(context.COLOR_BUFFER_BIT | context.DEPTH_BUFFER_BIT);

  // We need to ensure that the compositor has run before the drawing
  // takes place.  requestAnimationFrame was tried for this purpose,
  // but it did not produce the failing behaviour in crbug.com/105045.
  // A timeout of 100 ms was found to consistently reproduce the problem.
  setTimeout(draw, 100);
}
</script>
<canvas id="webgl-canvas" width="32px" height="32px"></canvas>
</body>
</html>
